//
//  PywLoginDataSource.m
//  PywSdk
//
//  Created by Jade on 4/7/16.
//  Copyright © 2016 zero. All rights reserved.
//

#import "PywLoginDataSource.h"
#import "PywMasonry.h"
#import "PywDeleteCacheApi.h"
#import "UIImage+Pyw.h"
typedef void(^DeleteCacheCompletion)(NSString *userName);
@interface PywLoginAccountCell : UITableViewCell
@property (nonatomic,strong) UIImageView *imgLine;
@property (nonatomic,strong) UIButton *deleteButton;
@property (nonatomic,copy) NSString *userName;
@property (nonatomic,strong) PywDeleteCacheApi *deleteCacheApi;
@property (nonatomic,copy) DeleteCacheCompletion deleteCacheCompletion;
@property (nonatomic,assign) BOOL isEdit;
@end
@implementation PywLoginAccountCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    [self.contentView addSubview:self.imgLine];
    [self.contentView addSubview:self.deleteButton];
    WEAK_SELF;
    [self.deleteButton mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.contentView.mas_centerY);
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-10);
        make.width.height.mas_equalTo(20);
    }];
}

- (void)setIsEdit:(BOOL)isEdit
{
    self.deleteButton.hidden = !isEdit;
}

#pragma mark - getter
- (PywDeleteCacheApi *)deleteCacheApi
{
    if (!_deleteCacheApi) {
        _deleteCacheApi = [PywDeleteCacheApi manager];
    }
    return _deleteCacheApi;
}

- (UIImageView *)imgLine
{
    if (!_imgLine) {
        _imgLine = [[UIImageView alloc] initWithFrame:CGRectMake(0,
                                                                     CGRectGetHeight(self.contentView.frame) - 1,
                                                                     CGRectGetWidth(self.contentView.frame),
                                                                     .5f)];
        _imgLine.backgroundColor = [UIColor colorWithRed:200/255.f
                                                       green:199/255.f
                                                        blue:204/255.f
                                                       alpha:1.f];
    }
    return _imgLine;
}

- (UIButton *)deleteButton
{
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteButton setImage:[UIImage imageWithName:@"icon_close_normal"]
                       forState:UIControlStateNormal];
        [_deleteButton addTarget:self
                          action:@selector(handleDelete:)
                forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}
#pragma mark - Actions
/**
 *  删除按钮
 *
 *  @param sender <#sender description#>
 */
- (void)handleDelete:(UIButton *)sender
{
    if (self.deleteCacheCompletion) {
        self.deleteCacheCompletion(self.userName);
    }
    [self.deleteCacheApi requestCheckPayPwdWithUserName:self.userName success:NULL fail:NULL];
}
@end



@interface PywLoginDataSource()
@property (nonatomic,copy) didSelectRow didSelectRow;
@property (nonatomic,strong) NSMutableArray *dataList;
@property (nonatomic,assign) BOOL isEdit;//是否可以编辑
@end

@implementation PywLoginDataSource
+ (instancetype)createTableDataSourceWithDataList:(NSArray *)dataList
                                           isEdit:(BOOL)isEdit
                                     didSelectRow:(didSelectRow)didSelectRow
{
    return [[[self class] alloc] initTableDataSourceWithDataList:dataList
                                                          isEdit:isEdit
                                                    didSelectRow:didSelectRow];
}

- (instancetype)initTableDataSourceWithDataList:(NSArray *)dataList
                                         isEdit:(BOOL)isEdit
                                   didSelectRow:(didSelectRow)didSelectRow
{
    self = [super init];
    if (self) {
        self.isEdit = isEdit;
        self.didSelectRow = didSelectRow;
        self.dataList = [[NSMutableArray alloc] initWithArray:dataList];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifiter = @"Cell";
    PywLoginAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:identifiter];
    if (cell == nil) {
        cell = [[PywLoginAccountCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:identifiter];
        cell.isEdit = self.isEdit;
        WEAK_SELF;
        cell.deleteCacheCompletion = ^(NSString *userName) {
            if ([weakSelf.dataList containsObject:userName]) {
                [weakSelf.dataList removeObject:userName];
            }
            if (weakSelf.deleteCacheCompletion) {
                weakSelf.deleteCacheCompletion(userName);
            }
        };
        
    }
    cell.textLabel.text = self.dataList[indexPath.row];
    cell.userName = self.dataList[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.didSelectRow) {
        self.didSelectRow(indexPath);
    }
}
@end
